﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

//员工帐套列表
//用来设置基本工资数据
namespace UI.ModSalary
{
    public partial class EmpSetsList : System.Web.UI.Page
    {
        EmpSetsBiz empSetsBiz = new EmpSetsBiz();
        SalaryTableBiz salaryTableBiz = new SalaryTableBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNoSets.Attributes.Add("onclick", "javascript:ShowLink('EmpSetsNo.aspx',800,600);");
                DefDicBiz defDicBiz = new DefDicBiz();
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                defDicBiz.InitDropDownList("Wage_SalaryType", ddlSalaryType, "1");
                new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
                //if (Session["user"] != null)
                //{
                //    //User user = Session["user"] as User;
                //    //iAccountID = user.AccountID;

                //}
                //else//请登陆
                //{
                //    Response.Write("你没有登录！");
                //    Response.End();
                //}


            }
        }

        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            string empCode = txtSeWord.Text.Trim();

            string deptID = ddlDeptID.SelectedValue.ToString();
            string setsID = ddlSetsID.SelectedValue.ToString();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                if (setsID == "" || setsID == "0")
                {
                    Utils.JSUtility.Alert(this, "请选择帐套");
                    return;
                }

            }

            BindGv(empCode, deptID, setsID);
        }

        protected void BindGv(string empCode, string deptID, string setsID)
        {
            DataTable dt = empSetsBiz.GetLists(empCode, deptID, setsID);
            int r = dt.Rows.Count;
            gvList.DataSource = dt;
            gvList.DataBind();
            if (r > 0)
            {
                lbAmount.Text = "总人数：" + r.ToString();
            }
        }
        //给指定人员 或指定部门中的所有正式员工设置工资帐套
        //改变帐套的时候，要删除旧有基础工资信息-----需要实现
        protected void btnSet_Click(object sender, EventArgs e)
        {
            string empCode = txtSeWord.Text.Trim();

            string deptID = ddlDeptID.SelectedValue.ToString();
            string setsID = ddlSetsID.SelectedValue.ToString();

            if (setsID == "" || setsID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择帐套");
                return;
            }
            if (string.IsNullOrWhiteSpace(empCode))
            {
                if (deptID == "" || deptID == "0")
                {
                    Utils.JSUtility.Alert(this, "请选择部门");
                    return;
                }
            }
            if (empSetsBiz.ConfigSets(empCode, deptID, setsID))
            { Utils.JSUtility.Alert(this, "设置成功"); }
            else
            { Utils.JSUtility.Alert(this, "设置失败"); }

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            bool flag = true;
            string empCode = txtSeWord.Text.Trim();

            string deptID = ddlDeptID.SelectedValue.ToString();
            string setsID = ddlSetsID.SelectedValue.ToString();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (empSetsBiz.IsExistEmpCode(empCode))
                    flag = empSetsBiz.DeleteOneInfo(empCode);
                else
                {
                    Utils.JSUtility.Alert(this, "工号不存在");
                    return;
                }
            }
            else
            {
                if (setsID != "0")
                    flag = empSetsBiz.DeleteDeptInfo(deptID, setsID);
                else
                {
                    Utils.JSUtility.Alert(this, "请选择帐套");
                    return;
                }
            }
            if (flag) Utils.JSUtility.Alert(this, "删除成功");
            else
                Utils.JSUtility.Alert(this, "删除失败");

        }

        protected void btnAddBook_Click(object sender, EventArgs e)
        {
            string typeID = ddlSalaryType.SelectedValue.ToString();
            if (typeID == "" || typeID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择类别");
                return;
            }
            ArrayList empCodeArray = new ArrayList();
            foreach (GridViewRow gvr in gvList.Rows)
            {
                CheckBox ch = (CheckBox)gvr.FindControl("CheckBox1");
                if (ch.Checked == true)
                {
                    string strTmp = this.gvList.DataKeys[gvr.DataItemIndex].Value.ToString();
                    empCodeArray.Add(strTmp);
                }
            }
            //加入Wage_SalaryBook
            if(salaryTableBiz.ConfigAddBooks(empCodeArray,typeID))
                  Utils.JSUtility.Alert(this, "加入成功");
            else
                Utils.JSUtility.Alert(this, "加入失败");

        }
        protected void btnMove_Click(object sender, EventArgs e)
        {

            string typeID = ddlSalaryType.SelectedValue.ToString();

            if (typeID == "" || typeID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择类别");
                return;
            }
            ArrayList empCodeArray = new ArrayList();
            foreach (GridViewRow gvr in gvList.Rows)
            {
                CheckBox ch = (CheckBox)gvr.FindControl("CheckBox1");
                if (ch.Checked == true)
                {
                    string strTmp = this.gvList.DataKeys[gvr.DataItemIndex].Value.ToString();
                                
                    empCodeArray.Add(strTmp);
                }
            }
            if (salaryTableBiz.ConfigDelBooks(empCodeArray))
                Utils.JSUtility.Alert(this, "移除成功");
            else
                Utils.JSUtility.Alert(this, "移除失败");
        }
        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i <= gvList.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)gvList.Rows[i].FindControl("CheckBox1");
                if (CheckBox2.Checked == true)
                {
                    cbox.Checked = true;
                }
                else
                {
                    cbox.Checked = false;
                }
            }
        }

        protected void btnSearchBook_Click(object sender, EventArgs e)
        {
            string typeID = ddlSalaryType.SelectedValue.ToString();
            if (typeID == "" || typeID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择类别");
                return;
            }


            DataTable dt = salaryTableBiz.GetBooks(typeID);
            int r = dt.Rows.Count;
            gvList.DataSource = dt;
            gvList.DataBind();
            if (r > 0)
            {
                lbAmount.Text = "总人数：" + r.ToString();
            }
        }

       





    }
}